<?php

use yii\db\Migration;

class m180917_125929_create_user_package extends Migration
{
    public function safeUp()
    {
        $this->createTable('{{%user_package}}', [
            'id' => $this->primaryKey(),
            'uid' => $this->integer()->notNull()->comment('用户编号'),
            'package_id' => $this->integer()->notNull()->comment('套餐编号'),
            'cost_price' => $this->decimal(20, 4)->notNull()->comment('成本价'),
        ]);
        $this->createIndex('fk_user_package_user1_idx', '{{%user_package}}', ['uid']);
        try {
            $this->addForeignKey('fk_user_package_user1', '{{%user_package}}', ['uid'], '{{%user}}', ['id']);
        } catch (Exception $e) {
            Yii::error($e->getMessage());
        }
        $this->createIndex('fk_user_package_package1_idx', '{{%user_package}}', ['package_id']);
        try {
            $this->addForeignKey('fk_user_package_package1', '{{%user_package}}', ['package_id'], '{{%package}}', ['id']);
        } catch (Exception $e) {
            Yii::error($e->getMessage());
        }
    }

    public function safeDown()
    {
        $this->dropTable('{{%user_package}}');
    }
}
